const WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
Page({
  data: {
    detail:{},
    spec:[],
    maskShow: false,
    specShow: false,
    specType: 0, //标注商品类型是普通商品还是套餐
    num: 1,
  },
  onLoad: function (options) {
    let that = this;
    if (options.scene) {
      let  str = decodeURIComponent(options.scene);
      let scene = [];
      if (str.indexOf('&') != -1) {
        let  arr = str.split('&');
        for (let  i = 0; i < arr.length; i++) {
          let  s = arr[i].split('=');
          scene[s[0]] = s[1];
        }
      }
      options.id = scene.id;
      options.specType = scene.specType;
    }
    that.setData({
      options: options,
      specType: options.specType || '0',
    })
    
  },
  onShow: function () {
    let that = this;
    app.confirmLaunch(function () {
      that.getInfo();
    })
  },
  getFormid(e) {
    app.getFormid(e);
  },
  // 获取商品信息
  getInfo(e){
    let that = this;
    let goods_id = that.data.options.id;
    let specType = that.data.specType;
    app.request(specType==0?'product/goodsDetail':'package/goodsDetail', { goods_id },'GET',false,function(res){
      wx.setStorageSync('goodsDetail', res.data.data.detail)
      if(res.data.data.goodsList){
        for(let i in res.data.data.goodsList){
          res.data.data.goodsList[i].detail.selected = 1;
        }
      }
      that.setData({
        pageShow: true,
        detail: res.data.data.detail,
        spec: res.data.data.specData || {},
        goodsList: res.data.data.goodsList || [],
        isCollect: res.data.data.isCollect,
        editor: WxParse.wxParse('content', 'html', res.data.data.detail.content, that, 5),
        shareLink: "/pages/poster/poster?id=" + goods_id + "&specType=" + specType,
      },()=>{
        specType == 0 ? that.getSpecIndex() : that.getTotalPrice()
      })
    })
  },
  // 获取规格信息
  getSpecIndex(e) {
    let that = this;
    let spec = that.data.spec;
    if(spec.length == 0){
      let specChoose = {};
      specChoose['stock_num'] = that.data.detail.stock;
      specChoose['goods_price'] = that.data.detail.goods_price;
      specChoose['title'] = '默认规格';
      that.setData({
        specChoose
      })
    }else{
      let specChoose = '';
      spec.spec_attr.map((v, k) => {
        v.spec_items.map((v1, k1) => {
          if (v1.selected == 1) {
            specChoose += v1.spec_value + '_'
          }
        })
      })
      specChoose = specChoose.substring(0, specChoose.length - 1)
      for (let i = 0; i < spec.spec_list.length; i++) {
        if (spec.spec_list[i].spec_sku_id == specChoose) {
          spec.spec_list[i].form['title'] = spec.spec_list[i].spec_sku_id
          that.setData({
            specChoose: spec.spec_list[i].form,
            num: spec.spec_list[i].form.stock_num > 0 ? 1 : 0,
          })
          break;
        }
      }
    }
  },
  // 分享
  share: function (e) {
    var that = this;
    that.setData({
      shareShow: true,
      maskShow: true,
    })
  },
  // 收藏
  collection(e){
    let that = this;
    let isCollect = !that.data.isCollect;
    let is_package = that.data.specType;
    let goods_id = that.data.detail.id;
    app.request('collect/addCollect', { goods_id, is_package }, 'GET', true, function (res) {
      wx.showToast({
        title: isCollect? '收藏成功' : '取消成功',
      })
      that.setData({
        isCollect: isCollect
      })
    })
  },
  // 显示规格
  specShow(e){
    let that = this;
    app.confirmTap(function () {
      let type = e.currentTarget.dataset.type;
      that.setData({
        type: type,
        maskShow: true,
        specShow: true,
      })
    })
  },
  // 隐藏分享和规格
  maskHide(e){
    let that = this;
    that.setData({
      maskShow:false,
      specShow:false,
      shareShow: false,
    })
  },
  // 数量加减
  numChange(e){
    let that = this;
    let type = e.currentTarget.dataset.type;
    let num = that.data.num;
    if(that.data.specType == 0){
      let specChoose = that.data.specChoose;
      switch (type) {
        case 'reduceSpec':
          num <= 1 ? 1 : num--;
          that.setData({
            num: num
          })
          break;
        case 'addSpec':
          num < specChoose.stock_num ? num++ : num = specChoose.stock_num
          that.setData({
            num: num
          })
          break;
      }
    }else{
      let index = e.currentTarget.dataset.index;
      let goodsList = that.data.goodsList;
      switch (type) {
        case 'reduceMenu':
          goodsList[index].detail.number <= 1 ? 1 : goodsList[index].detail.number--;
          that.setData({
            goodsList: goodsList
          })
          break;
        case 'addMenu':
          goodsList[index].detail.number < goodsList[index].detail.stock ? goodsList[index].detail.number++ : goodsList[index].detail.number = goodsList[index].detail.stock
          that.setData({
            goodsList: goodsList
          })
          break
      }
      that.getTotalPrice()
    }
  },
  // 规格选择
  specChoose(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let idx = e.currentTarget.dataset.idx;
    let spec = that.data.spec;
    spec.spec_attr[index].spec_items.map((v,k)=>{
      v.selected = idx==k?1:0;
    })
    that.setData({
      spec: spec,
    },()=>{
      that.getSpecIndex();
    })
  },
  // 套餐选择
  menuChoose(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let goodsList = that.data.goodsList;
    goodsList[index].detail.selected = goodsList[index].detail.selected==0? 1 : 0;
    that.setData({
      goodsList: goodsList
    },()=>{
      that.getTotalPrice()
    })
  },
  // 计算总价
  getTotalPrice(e){
    let that = this;
    let goodsList = that.data.goodsList;
    let totalPrice = 0;
    let menuSure = false;
    for (let k in goodsList){
      if (goodsList[k].detail.selected) {
        menuSure = true;
        totalPrice += parseFloat(goodsList[k].detail.goods_price) * goodsList[k].detail.number
      }
    }
    that.setData({
      menuSure: menuSure,
      totalPrice: totalPrice.toFixed(2)
    })
  },
  // 确认下单或加入购物车
  orderSure(e){
    let that = this;
    // let data = {
    //   goodsData: []
    // };
    let data=[]
    if(that.data.specType == 0){
      let dataItem = {
        pack_id: '0',
        goods_id: that.data.detail.id,
        spec_id: that.data.specChoose.title || '',
        spec_num: that.data.num,
      };
      // data.goodsData.push(dataItem)
      data.push(dataItem)
    }else{
      let goodsList = that.data.goodsList;
      for (let i in goodsList){
        if (goodsList[i].detail.selected == 1) {
          let dataItem = {
            pack_id: goodsList[i].pack_id,
            goods_id: goodsList[i].goods_id,
            spec_id: goodsList[i].detail.spec_value || '',
            spec_num: goodsList[i].detail.number,
          };
          data.push(dataItem)
        }
      }
    }
    let type = that.data.type;
    if(type =='cart'){
      let city_id = wx.getStorageSync('city_id');
      let goodsInfo = encodeURIComponent(JSON.stringify(data));
      app.request('shopcar/addShopcar', { city_id, goodsInfo }, 'GET', false, function (res) {
        wx.showToast({
          title: res.data.data,
        })
        that.setData({
          maskShow: false,
          specShow: false,
        })
      },function(){},function(){
        that.setData({
          maskShow: false,
          specShow: false,
        })
      })
    }else{
      wx.navigateTo({
        url: '/pages/order/orderSure/orderSure?data=' + JSON.stringify(data),
      })
      that.setData({
        maskShow: false,
        specShow: false,
      })
    }
   
  },
  onReachBottom: function () {

  },
  // 分享
  onShareAppMessage: function () {
    let that = this;
    return{
      imageUrl: that.data.detail.images[0].image.file_path,
    }
  }
})